import { fromJS } from 'immutable'
import * as actionTypes from './actionTypes';

const defaultState = fromJS({
  topicList: [],
  articleList: [],
  recommendList: [],
  articlePage: 1,
  showScroll: false
});

export default (state = defaultState, action) => {
  switch(action.type) {
    case actionTypes.CHANGE_HOME_DATA:
      return changHomeData(state, action);
    case actionTypes.ADD_HOME_LIST:
      return addHomeList(state, action);
    case actionTypes.TOGGLE_SCROLL_SHOW:
      return state.set('showScroll', action.show)
    default:
      return state;
  }
}

const changHomeData = (state, action) => {
  return state.merge({
    topicList: fromJS(action.topList),
    articleList: fromJS(action.articleList),
    recommendList: fromJS(action.recommendList)
  })
}

const addHomeList = (state, action) => {
  return state.merge({
    'articleList': state.get('articleList').concat(fromJS(action.list)),
    'articlePage': action.page
  })
}